import { env } from '@/config.js';
let baseURL = env.serviceDesk.baseURL;

const RequestService = (method = 'GET', path, params, needToken = true) => {
	let { access_token } = uni.getStorageSync("serviceUserInfo");
	if (env.hasChangEnv) {
		let serviceBaseURL = env.serviceBaseURL
		let apiEnv = uni.getStorageSync('apiEnv');
		if (apiEnv) {
			baseURL = serviceBaseURL[apiEnv]
		} else {
			baseURL = serviceBaseURL[env.env]
		}
	}
	uni.showLoading({
		title: '加载中',
		mask: true
	});
	console.log(`${baseURL}${path}`, params, '接口api', env.env)
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseURL}${path}`,
			method,
			data: {
				...params
			},
			header: {
				'Access-Token': needToken ? access_token : '',
			},
			success: (res) => {
				// if (res.statusCode == 401) {
				// 	uni.showToast({
				// 		title: '操作已超时，请重新进入工单页面',
				// 		icon: 'none',
				// 		duration: 3000,
				// 		complete: () => {
				// 			setTimeout(() => {
				// 				uni.reLaunch({
				// 					url: '/pages/index/newIndex'
				// 				})
				// 			}, 3000)
				// 		}
				// 	})
				// 	reject();
				// }
				if (res.statusCode != 200) {
					uni.showToast({
						title: res.data.error_descrip,
						icon: 'none'
					})

					reject(res.data.error_descrip)
				} else {
					resolve(res)
				}
				console.log('111', res)

			},
			fail: (err) => {
				uni.showToast({
					title: '获取服务台鉴权出错，请稍后再试',
					icon: 'none'
				})
				reject(err);
			},
			complete() {
				uni.hideLoading();
			}
		})
	})
}
/**
 * 获取登录token
 * @params {
	 uid,
	 login
 }
 */
export function GetServiceToken(params) {
	return RequestService("GET", "/api/auth/get_tokens", params, false)
}
/**
 * 获取我的订单列表数据
 * @param {
	 filters,
	 limit,
	 offset,
	 order,
	 
 }
 */
export function GetTicketList(params) {
	/**
	 * 将 params.serachValue 和 params.status =>
	 * 	['|',['number', 'like', '1006'], ['name', 'like', '1006'], ['stage_id', '=', 1]]
	 */
	const format = (searchValue, status) => `['|',['number', 'like', '${searchValue}'], ['name', 'like', '${searchValue}']${status ? `, ['stage_id', '=', ${status}]` : ''}]`
	const filters = format(params.searchValue, params.status)
	delete params.searchValue
	delete params.status
	return RequestService("GET", "/api/helpdesk.ticket", {
		...params,
		filters
	})
}
/**
 * 我的订单详情获取
 * id
 */
export function GetTicketDetail(id) {
	return RequestService("GET", `/api/helpdesk.ticket/${id}`)
}
/**
 * 我的工单详情-处理结果
 * @param {
	 id
	 limit,
	 offset,
 }
 */
export function GetProcessResult(params) {
	return RequestService("POST", `/oneapp/get_message`, params)
}
/**
 * 获取省份
 * @params {
	 params: {
		uid 
	 } 
 }
 */
export function GetProvinces(params) {
	return RequestService("POST", '/oneapp/get_province', params)
}
/**
 * 根据省份和品牌获取业务类型
 * @params {
	 uid,
	 province,
	 brand
 }
 */
export function GetBusinessTypes(params) {
	return RequestService("POST", '/oneapp/get_work_type', {
		...params,
		brand: env.brand == 'anneng' ? '安能' : '光伏星'
	})
}
/**
 * 工单创建(保存草稿时 调用)
 * @param {
	 name, 标题
	 description, 说明
	 agentuser_id, 需求人(当前用户鉴权时返回的id)
	 user_id, 处理人(业务类型选择时带的)
	 attachment_ids, 附件数据
 }
 */
export function AddTicket(params) {
	return RequestService('POST', '/api/helpdesk.ticket', params)
}
/**
 * 工单更新
 * @param {
	 name,
	 description,
	 agentuser_id,
	 att
 }
 */
export function UpdateTicket(params) {
	return RequestService('PUT', `/api/helpdesk.ticket/${params.id}`, params)
}
/**
 *工单提交 
 @params {
	 id
 }
 */
export function SubmitTicket(params) {
	return RequestService('PUT', `/api/helpdesk.ticket/${params.id}/button_submit_tj `)
}
/**
 * 工单删除
 * @param {
	 id: 工单id
 }
 */
export function DeleteTicket(id) {
	return RequestService('DELETE', `/api/helpdesk.ticket/${id}`)
}
/**
 * 工单评价
 * @param {
	 id
	 right_score 满意度
	 right_comment 评价
 }
 */
export function SenEvaluation(params) {
	const { id, right_comment, right_score } = params;
	return RequestService('PUT', `/api/helpdesk.ticket/${id}/_button_finsh`, {
		right_score,
		right_comment
	})
}
/**
 *工单驳回 
 @param {
	 id,
	 reason_results 驳回原因
 }
 */
export function RejectTicket(params) {
	const { id, reason_results } = params
	return RequestService('PUT', `/api/helpdesk.ticket/${id}/_btn_not_finsh`, {
		reason_results,
	})
}


/**
 * 获取备案类型
 */
export function GetFilingType(params) {
	return RequestService("GET", '/oneapp/get_filing_type', params)
}
